package com.zzh.base.component;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.zzh.base.R;
import com.zzh.base.utils.ActivityUtil;
import com.zzh.base.utils.AutoUtils;
import com.zzh.tools.toast.ToolsToast;

public class AppActivity extends AppCompatActivity {

    protected boolean isAutoSize = false;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_app);
        init();
    }

    private void init() {
        ActivityUtil.getInstance().addActivity(this);
        initView();
    }

    private void initView() {
        updateTitleView(titleView);
        updateRootView(rootView);
    }

    public void updateTitleView(View view) {
        RelativeLayout titleRoot = findViewById(R.id.title_root);
        if (view != null) {
            titleRoot.removeAllViews();
            titleRoot.addView(view);
        }
        if (isAutoSize) {
            AutoUtils.auto(titleRoot);
        }
    }

    public void updateRootView(View view) {
        LinearLayout baseContainer = findViewById(R.id.base_container);
        if (baseContainer.getChildCount() > 0)
            baseContainer.removeAllViews();
        baseContainer.addView(view);
    }

    private View rootView;

    protected void configRootView(View view) {
        rootView = view;
    }

    private View titleView;

    protected void configTitleView(View view) {
        titleView = view;
    }

    protected void hiddenTitle() {
        findViewById(R.id.title_root).setVisibility(View.GONE);
    }

    protected void showTitle() {
        findViewById(R.id.title_root).setVisibility(View.VISIBLE);
    }


    private View loadingView;

    protected void configLoading(View view) {
        loadingView = view;
    }

    protected void showToast(String msg) {
        ToolsToast.mustToast(msg);
    }

    protected void showLoading(boolean isLimitClick) {
        LinearLayout mLoadingView = findViewById(R.id.base_loading);
        if (mLoadingView.getChildCount() <= 0) {
            if (loadingView == null) {
                mLoadingView.addView(LayoutInflater.from(this).inflate(R.layout.default_loading, null));
            } else {
                mLoadingView.addView(loadingView);
            }
            if (isAutoSize) {
                AutoUtils.auto(mLoadingView);
            }
        }
        mLoadingView.setVisibility(View.VISIBLE);
        mLoadingView.setClickable(isLimitClick);
    }

    protected void hintLoading() {
        findViewById(R.id.base_loading).setVisibility(View.GONE);
    }

    protected void configAutoSize(boolean isAutoSize) {
        this.isAutoSize = isAutoSize;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ActivityUtil.getInstance().removeActivity(this);
    }

}
